package com.syni.mdd.yhd.common.all.utils;

import lombok.extern.log4j.Log4j2;
import org.springframework.util.ResourceUtils;

import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * @className OutImageUtils
 * @description TOO
 * @Author cfx
 * @DATE 2019/4/22 12:42
 * @VERSION 1.0
 **/
@Log4j2
public class OutImageUtils {
    public static Map<String,Object> imageContentType = new HashMap<>();

    static {
        //初始化图片
        imageContentType.put("jpg","image/jpeg");
        imageContentType.put("jpeg","image/jpeg");
        imageContentType.put("png","image/png");
        imageContentType.put("tif","image/tiff");
        imageContentType.put("tiff","image/tiff");
        imageContentType.put("ico","image/x-icon");
        imageContentType.put("bmp","image/bmp");
        imageContentType.put("gif","image/gif");
    }

    public static void drawImg(String imgPath, Map<String,Object> data,
                               Map<String,Integer[]> imageDataXyCoordinate,
                               HttpServletResponse response){
        Font font = new Font("微软雅黑", Font.BOLD, 35);                     //水印字体
        Color color = new Color(226,11,85);
        //水印图片色彩以及透明度
        File srcImgFile;
        try {
            srcImgFile = ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX + imgPath);
            if(srcImgFile.exists()){
                String fileName = srcImgFile.getName();
                String fileType = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
                response.setHeader("Content-type", imageContentType.get(fileType).toString());
                Image srcImg = ImageIO.read(srcImgFile);//文件转化为图片
                int srcImgWidth = srcImg.getWidth(null);//获取图片的宽
                int srcImgHeight = srcImg.getHeight(null);//获取图片的高
                BufferedImage bufImg = new BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB);
                //加水印
                Graphics2D g = bufImg.createGraphics();
                g.drawImage(srcImg, 0, 0, srcImgWidth, srcImgHeight, null);
                g.setColor(color); //根据图片的背景设置水印颜色
                g.setFont(font);
                //设置城市打卡数
                Map<String,Integer> cityNumDkData = (Map<String,Integer>) data.get("eachCityDkNum");
                if(cityNumDkData.size() > 1){
                    //设置两个城市
                    for(Map.Entry<String,Integer> entryContent : cityNumDkData.entrySet()) {
                        if(imageDataXyCoordinate.containsKey(entryContent.getKey())){
                            Integer[] xyCoordinate = imageDataXyCoordinate.get(entryContent.getKey());
                            g.drawString(entryContent.getValue().toString(), xyCoordinate[0], xyCoordinate[1]);
                        }
                    }
                }else {
                    //设置一个城市
                    Integer[] xyCoordinate = imageDataXyCoordinate.get("珠海广州");
                    for(Map.Entry<String,Integer> entryContent : cityNumDkData.entrySet()) {
                        g.drawString(entryContent.getValue().toString(), xyCoordinate[0], xyCoordinate[1]);
                    }
                }
                //设置基础数据
                for(Map.Entry entry : data.entrySet()){
                    if(imageDataXyCoordinate.containsKey(entry.getKey())){
                        Integer[] xyCoordinate = imageDataXyCoordinate.get(entry.getKey());
                        String drawContent = entry.getValue() == null ? "": entry.getValue().toString();
                        g.drawString(drawContent,xyCoordinate[0],xyCoordinate[1]);
                    }
                }
                //新图片
                //File imgFile = new File("D:/img/ellBackground.png");
                // 获取层图
                //BufferedImage waterImg = ImageIO.read(imgFile);
                //int waterImgWidth = waterImg.getWidth();// 获取层图的宽度
                //int waterImgHeight = waterImg.getHeight();// 获取层图的高度
                //叠加图层
                //g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 1.0f));
                // 绘制
                //g.drawImage(waterImg,0, 0, waterImgWidth, waterImgHeight, null);
                g.dispose();
                bufImg.flush();
                ByteArrayOutputStream bs = new ByteArrayOutputStream();
                ImageOutputStream imageOutputStream = ImageIO.createImageOutputStream(bs);
                ImageIO.write(bufImg, fileType,imageOutputStream);
                InputStream inputStream = new ByteArrayInputStream(bs.toByteArray());
                outImg(inputStream,response);
            }
        } catch (FileNotFoundException e) {
            log.error(e.getMessage(),e);
        } catch (IOException e) {
            log.error(e.getMessage(),e);
        }
    }

    public static BufferedImage watermark(File file, File waterFile, int x, int y, float alpha) throws IOException {
        // 获取底图
        BufferedImage buffImg = ImageIO.read(file);
        // 获取层图
        BufferedImage waterImg = ImageIO.read(waterFile);
        // 创建Graphics2D对象，用在底图对象上绘图

        Graphics2D g2d = buffImg.createGraphics();
        int waterImgWidth = waterImg.getWidth();// 获取层图的宽度
        int waterImgHeight = waterImg.getHeight();// 获取层图的高度
        // 在图形和图像中实现混合和透明效果
        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
        // 绘制
        g2d.drawImage(waterImg, x, y, waterImgWidth, waterImgHeight, null);
        g2d.dispose();// 释放图形上下文使用的系统资源
        return buffImg;
    }

    public static void outImg(InputStream inputStream, HttpServletResponse response){
        OutputStream os;
        try {
            os = response.getOutputStream();
            byte[] b = new byte[1024];
            while(inputStream.read(b)!= -1) {
                os.write(b);
            }
            inputStream.close();
            os.flush();
            os.close();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }

}
